import pyglet
from pyglet.gui import PushButton
import os

import resouces


class SaveLabel:
    def __init__(self, x,y,num,batch,pic_resource,pointer,scene_manager,window,buttons):
        self.buttons = buttons
        self.scene_manager = scene_manager
        self.pointer = pointer
        self.num=num
        self.pic_resource = pic_resource
        self.batch = batch
        self.window = window
        self.x=x
        self.y=y

        self.label=pyglet.text.Label("SAVE"+str(self.num),font_name='Times New Roman',font_size=50,x=x,y=y+100,batch=batch,group=pyglet.graphics.Group(order=3))
        self.savelabel = pyglet.sprite.Sprite(pic_resource["savelabel"],x=x,y=y,batch=batch,group=pyglet.graphics.Group(order=2))

        try:
            print(resouces.get_resource_path("saves\save"+str(num)+".png"))
            self.scr=pyglet.image.load(resouces.get_resource_path("saves\save"+str(num)+".png"))
            self.screenshot_sprite=pyglet.sprite.Sprite(self.scr,x=x+354,y=y+50,batch=batch,group=pyglet.graphics.Group(order=3))
        except FileNotFoundError:
            pass

        self.savebtn = PushButton(x+225, y+117-157+40, pic_resource["savebtn_pressed"],pic_resource["savebtn_unpressed"], pic_resource["savebtn_hover"],batch=batch,group=pyglet.graphics.Group(order=3))
        self.loadbtn = PushButton(x+70, y+117-157+40, pic_resource["loadbtn_pressed"],pic_resource["loadbtn_unpressed"], pic_resource["loadbtn_hover"],batch=batch,group=pyglet.graphics.Group(order=3))

        self.savebtn.set_handler('on_release', self.save_btn_handler)
        self.loadbtn.set_handler('on_release', self.load_btn_handler)
        self.p = resouces.read_saves('save' + str(num))
        self.isLoadHandler=False
        if self.p != -1:
            # 读取成功
            if not self.isLoadHandler:
                self.window.push_handlers(self.loadbtn)
            #self.buttons.append(self.loadbtn)
        self.window.push_handlers(self.savebtn)
        #self.buttons.append(self.savebtn)


    def update_num(self,num):
        self.num=num
        self.p = resouces.read_saves('save' + str(self.num))
        if self.p == -1:
            # 读取失败
            print("remove")
            print(num)
            if self.isLoadHandler:
                self.window.remove_handlers(self.loadbtn)

        else:
            if not self.isLoadHandler:
                self.window.push_handlers(self.loadbtn)

        self.label.text = "SAVE"+str(self.num)
        try:
            self.screenshot_sprite.delete()
        except:
            pass

        try:
            self.scr=pyglet.image.load(resouces.get_resource_path("saves\save"+str(num)+".png"))
            self.screenshot_sprite=pyglet.sprite.Sprite(self.scr,x=self.x+354,y=self.y+50,batch=self.batch,group=pyglet.graphics.Group(order=3))
        except FileNotFoundError:
            pass

    def ok_btn_handler(self,button):
        print("ready to change")
        print(self.type)
        self.ok_sprite.delete()
        self.fail_label.delete()
        self.ok_btn.x = 10000  # 隐藏

        self.window.remove_handlers(self.ok_btn)

        if self.type==1:
            print("switch")
            # game_scene = GameScene(self.scene_manager, self.p)
            self.p = resouces.read_saves('save' + str(self.num))
            print("selfp:",self.p)
            self.scene_manager.switch_scene("game", self.p)
        else:
            src="saves\screenshot_tmp_ok.png"
            dst="saves\save" + str(self.num) + ".png"
            os.system(f"copy {src} {dst}")

            self.update_num(self.num)
    def destroy(self):
        self.window.remove_handlers(self.loadbtn)
        self.window.remove_handlers(self.savebtn)



    def save_btn_handler(self,button):
        self.type = 0  # 说明在save模式下
        resouces.output_saves("save"+str(self.num), str(self.pointer))

        self.ok_sprite=pyglet.sprite.Sprite(self.pic_resource["ok_sp"],x=self.scene_manager.get_cx()+1920/2-500/2,y=self.scene_manager.get_cy()+1080/2-200/2,batch=self.batch,group=pyglet.graphics.Group(order=4))
        self.fail_label = pyglet.text.Label("confirm", self.scene_manager.get_cx() + 1920 / 2, self.scene_manager.get_cy() + 600, anchor_x="center", anchor_y="center",font_size=20,color=(0,0,255,255),batch=self.batch,group=pyglet.graphics.Group(order=4))
        try:
            self.ok_btn.x = self.scene_manager.get_cx() + 1920 / 2 - 130 / 2
            self.window.push_handlers(self.ok_btn)
        except:
            self.ok_btn = PushButton(self.scene_manager.get_cx() + 1920 / 2 - 130 / 2, self.scene_manager.get_cy() + 500, self.pic_resource["ok_sp_btn_B1"],
                                     self.pic_resource["ok_sp_btn_B1"], batch=self.batch,
                                     group=pyglet.graphics.Group(order=5))

            self.ok_btn.set_handler('on_release', self.ok_btn_handler)
            self.window.push_handlers(self.ok_btn)
            self.buttons.append(self.ok_btn)




    def load_btn_handler(self,button):
        self.type = 1 # 说明在load模式下
        self.ok_sprite = pyglet.sprite.Sprite(self.pic_resource["ok_sp"], x=self.scene_manager.get_cx() + 1920 / 2 - 500 / 2,
                                              y=self.scene_manager.get_cy() + 1080 / 2 - 200 / 2, batch=self.batch,group=pyglet.graphics.Group(order=4))

        if self.p ==-1:
            self.fail_label = pyglet.text.Label("FAIL", self.scene_manager.get_cx() + 1920 / 2, self.scene_manager.get_cy() + 600, anchor_x="center", anchor_y="center",font_size=20,color=(0,0,255,255),batch=self.batch,group=pyglet.graphics.Group(order=4))
        else:
            self.fail_label = pyglet.text.Label("confirm", self.scene_manager.get_cx() + 1920 / 2, self.scene_manager.get_cy() + 600, anchor_x="center", anchor_y="center",font_size=20,color=(0,0,255,255),batch=self.batch,group=pyglet.graphics.Group(order=4))

        try :
            self.ok_btn.x=self.scene_manager.get_cx() + 1920 / 2 - 130 / 2
            self.window.push_handlers(self.ok_btn)
        except:
            self.ok_btn = PushButton(self.scene_manager.get_cx() + 1920 / 2 - 130 / 2, self.scene_manager.get_cy() + 500, self.pic_resource["ok_sp_btn_B1"],
            self.pic_resource["ok_sp_btn_B1"], batch=self.batch,group=pyglet.graphics.Group(order=5))

            self.ok_btn.set_handler('on_release', self.ok_btn_handler)
            self.window.push_handlers(self.ok_btn)
            self.buttons.append(self.ok_btn)
